import {Pipe, PipeTransform} from '@angular/core';

@Pipe({
  name: 'reportsFilter'
})
export class ReportsFilterPipe implements PipeTransform {

  transform(list: any[], subjectKW: string, gradeKW: string, classKW: string, typeKW: string, fromKW: string, toKW: string): any {
    return list.filter(item => {
      let fields = ['create', 'gradeName', 'className', 'type', 'subject'];
      let flag_create = true;
      let flag_gradeName = true;
      let flag_className = true;
      let flag_type = true;
      let flag_subject = true;
      //console.log(item);
      fields.map(function (field, index) {
        if (field == 'create') {
          if (fromKW && toKW) {//都有效
            let itemFieldValue = item[field];
            let val = new Date(itemFieldValue);
            let val_from = new Date(fromKW);
            let val_to = new Date(toKW);
            flag_create =  (val.getTime()>val_from.getTime() && val.getTime()<val_to.getTime())? true:false;
          }
          if (!fromKW && toKW) {//截止时间有效
            let itemFieldValue = item[field];
            let val = new Date(itemFieldValue);
            let val_to = new Date(toKW);
            flag_create =  (val.getTime()<val_to.getTime())? true:false;
          }
          if (fromKW && !toKW) {//开始时间有效
            let itemFieldValue = item[field];
            let val = new Date(itemFieldValue);
            let val_from = new Date(fromKW);
            flag_create =  (val.getTime()>val_from.getTime())? true:false;
          }
        }
        if(field=='gradeName'){
          let itemFieldValue = item[field];
          flag_gradeName =  gradeKW ? itemFieldValue.indexOf(gradeKW) >= 0 : true  ;
        }
        if(field=='className'){
          let itemFieldValue = item[field];
          flag_className = classKW ?  itemFieldValue.indexOf(classKW) >= 0  : true;
        }
        if(field=='type'){
          let itemFieldValue = item[field];
          flag_type = typeKW ? itemFieldValue.indexOf(typeKW) >= 0 : true ;
        }
        if(field=='subject'){
          let itemFieldValue = item[field];
          flag_subject = subjectKW ? itemFieldValue.indexOf(subjectKW) >= 0 : true ;
        }
      });
      //console.log("id: "+item['id']+"--flag--"+flag_create && flag_gradeName && flag_className && flag_type && flag_subject);
      return flag_create && flag_gradeName && flag_className && flag_type && flag_subject;
      /*let itemFieldValue = item[field].toString().toLowerCase();
       return itemFieldValue.indexOf(keyWord[0]) >= 0;*/
    })
  }
}
